Page({
	data: {
	  cartItems: [],
	  totalPrice: 0,
	  totalCount: 0,
	  isAllSelected: false,
	  isEmpty: true
	},
	onShow() {
	  this.loadCartData();
	},
	// 加载购物车数据
	loadCartData() {
	  const cartItems = wx.getStorageSync('cartItems') || [];
	  const isEmpty = cartItems.length === 0;
	  // 检查是否全选
	  const isAllSelected = cartItems.length > 0 && cartItems.every(item => item.selected);
	  this.setData({
		cartItems,
		isEmpty,
		isAllSelected
	  });
	  this.calculateTotal();
	},
  
	// 计算总价和总数量
	calculateTotal() {
	  let totalPrice = 0;
	  let totalCount = 0;
	  
	  this.data.cartItems.forEach(item => {
		if (item.selected) {
		  totalPrice += item.price * item.quantity;
		  totalCount += item.quantity;
		}
	  });
  
	  this.setData({
		totalPrice: totalPrice.toFixed(2),
		totalCount
	  });
	},
  
	// 选择单个商品
	selectItem(e) {
	  const { id } = e.currentTarget.dataset;
	  const { cartItems } = this.data;
	  
	  const newCartItems = cartItems.map(item => {
		if (item.id === id) {
		  item.selected = !item.selected;
		}
		return item;
	  });
  
	  const isAllSelected = newCartItems.every(item => item.selected);
	  this.setData({
		cartItems: newCartItems,
		isAllSelected
	  });
	  wx.setStorageSync('cartItems', newCartItems);
	  this.calculateTotal();
	},
	// 全选/取消全选
	selectAll() {
	  const { cartItems, isAllSelected } = this.data;
	  const newCartItems = cartItems.map(item => ({
		...item,
		selected: !isAllSelected
	  }));
  
	  this.setData({
		cartItems: newCartItems,
		isAllSelected: !isAllSelected
	  });
	  wx.setStorageSync('cartItems', newCartItems);
	  this.calculateTotal();
	},
	// 增加数量
	increaseQuantity(e) {
	  const { id } = e.currentTarget.dataset;
	  const { cartItems } = this.data;
	  
	  const newCartItems = cartItems.map(item => {
		if (item.id === id) {
		  // 限制最大数量为99
		  item.quantity = Math.min(item.quantity + 1, 99);
		}
		return item;
	  });
	  this.setData({ cartItems: newCartItems });
	  wx.setStorageSync('cartItems', newCartItems);
	  this.calculateTotal();
	},
	// 减少数量
	decreaseQuantity(e) {
		const { id } = e.currentTarget.dataset;
		const { cartItems } = this.data;
		
		const item = cartItems.find(item => item.id === id);
		if (item.quantity <= 1) {
		  // 当数量为1时，直接调用删除方法
		  this.deleteItem(e);
		  return;
		}
		// 数量大于1时，正常减少
		const newCartItems = cartItems.map(item => {
		  if (item.id === id) {
			item.quantity -= 1;
		  }
		  return item;
		});
		this.setData({ cartItems: newCartItems });
		wx.setStorageSync('cartItems', newCartItems);
		this.calculateTotal();
	  },	
	// 删除确认
	showDeleteConfirm(id) {
	  wx.showModal({
		title: '提示',
		content: '确定要删除这个商品吗？',
		success: (res) => {
		  if (res.confirm) {
			this.deleteItem(id);
		  }
		}
	  });
	},
	// 删除商品
	deleteItem(e) {
		const { id } = e.currentTarget.dataset;	
		wx.showModal({
		  title: '提示',
		  content: '确定要删除这个商品吗？',
		  success: (res) => {
			if (res.confirm) {
			  const { cartItems } = this.data;
			  const newCartItems = cartItems.filter(item => item.id !== id);
			  
			  this.setData({
				cartItems: newCartItems,
				isEmpty: newCartItems.length === 0
			  });
			  wx.setStorageSync('cartItems', newCartItems);
			  this.calculateTotal();
			  wx.showToast({
				title: '删除成功',
				icon: 'success'
			  });
			}
		  }
		});
	  },
	// 结算
	checkout() {
	  const selectedItems = this.data.cartItems.filter(item => item.selected);
	  
	  if (selectedItems.length === 0) {
		wx.showToast({
		  title: '请选择商品',
		  icon: 'none'
		});
		return;
	  }
  
	  // 这里可以跳转到订单确认页面
	  wx.navigateTo({
		url: '/pages/order/confirm/confirm',
		success: () => {
		  // 可以通过事件通道传递选中的商品数据
		  const eventChannel = this.getOpenerEventChannel();
		  eventChannel.emit('acceptSelectedItems', { items: selectedItems });
		}
	  });
	}
  });